#include <iostream>
#include <vector>
using namespace std;

const int N = 1e5 + 7;
int ep, n, m, pos = 1;
int save[105];

struct NODE
{
    vector<int> next;
} nodes[N];

int main()
{
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
    {
        int cnt;
        cin >> cnt;
        while (cnt--)
        {
            int next;
            cin >> next;
            nodes[i].next.push_back(next);
        }
    }
    while (m--)
    {
        int opr, p;
        cin >> opr >> p;
        if (opr == 0)
        {
            pos = nodes[pos].next[p - 1];
        }
        else if (opr == 1)
        {
            save[p] = pos;
            cout << pos << endl;
        }
        else
        {
            pos = save[p];
        }
    }
    cout << pos;
    return 0;
}